<!DOCTYPE html>

<!--
This is the chrome extension background script.
The script is executed on browser startup
-->

<html>
<head>
	<script src="lib/require.js"></script>
</head>
<body>
<script>
    require({
        baseUrl: ''
    });

	/**
     * Sync the local site list with the remote site list
     */
     require(['config', 'log/background', 'se/api', 'lib/urlparse', 'db', 'db/sites'],
     function(Config, Log, SeApi, UrlParser, Db, Sites){

         var onError = function(msg){
             Log.error(msg);
         }
         
         //update (if required) the site list
         var sites = new Sites.SitesDb(Db.getDb(Config.SettingsDb));
         var api = new SeApi.Api('',Config.ApiKey);

         sites.ensureCreated(null,{
             success:function(){
                 api.Sites({}, SeApi.from(0)).each({
                 step:function(seSite){
                        var host = UrlParser(seSite.site_url).host; 

                         Log.info(host);

                        var setSiteDetails = function(tx, site){
                             //alert('saving');
                             site.Host    = host; 
                             site.SiteUrl = seSite.site_url;
                             site.ApiUrl  = seSite.api_endpoint;
                             site.save(null,{
                                 error:onError
                             });
                         }

                         sites.getByHost(null, host, {
                             success: setSiteDetails,
                             notFound:function(){
                                 Log.info('Adding new site: ' + host);
                                 sites.createSite(null,{ success:setSiteDetails })
                             },
                             error:onError
                         });
                         
                 }
                 });
             },
             error:onError
         });

     });

    require(['db', 'db/sites', 'lib/jquery'],
    function(Db, Sites){
        /**
         * This is required so that an inject page can access the StackedOdds database.
         * 
         * @TODO needs neating up
         */
        function onRequest(req, sender, callback) {
            if ( req.obj == 'ajax' ){
                $.ajax({
                    url:req.url,
                    success:callback
                });
            }
            if (req.obj && req.obj == 'site') {
                var db = new Sites.SitesDb(Db.getDb(req.db));
                //db[req.func].apply(db, req.args);
                if ( req.func == 'getByHost' ){
                    db.getByHost(null,
                                req.args[0],{
                                    success:function(t, s){ callback(s); }
                                });
                }
                if ( req.func == 'saveSite' ){
                    db.saveSite(null, req.args[0],{success:callback});
                }
            }

            if ( req.obj == 'tab' ){
                chrome.tabs.executeScript(sender.tab.id, {file:req.url}, callback);
            }
            if ( req.obj == 'set' ){
            }

        };
        chrome.extension.onRequest.addListener(onRequest);
    });

</script>
</body>
</html>

